package design_patterns_and_best_practices_in_java;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2019-11-14 下午2:29
 */
public class Chapter3MemorandumMode {
    public static void main(String[] args) {
        CarCaretaker caretaker = new CarCaretaker();
        caretaker.runMechanicTest();
    }
}

class CarCaretaker {
    public void runMechanicTest() {
        CarOriginator.Memeto savedState = new CarOriginator.Memeto("");
        CarOriginator originator = new CarOriginator();
        originator.setState("state1");
        originator.setState("state2");
        savedState = originator.saveState();
        originator.setState("state3");
        originator.restoreState(savedState);
        System.out.println("final state is " + originator.getState());
    }
}

class CarOriginator {
    private String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Memeto saveState() {
        return new Memeto(this.state);
    }

    public void restoreState(Memeto memeto) {
        this.state = memeto.getState();
    }

    /**
     * Memeto class
     */
    public static class Memeto {
        private final String state;

        public Memeto(String state) {
            this.state = state;
        }

        private String getState() {
            return state;
        }
    }
}
